const { json } = require('express');
var express = require('express');
var router = express.Router();

const MoviesModel = require('../model/movies');
const DetailsModel = require('../model/details');

/* GET home page. */
router.get('/list', function(req, res, next) {
  MoviesModel.find({}, {_id: 0, movieid: 1}, (err, docids) => {
    if (err) throw err;
    const count = docids.length;
    const ids = [];
    docids.forEach(item => {
      ids.push(JSON.parse(JSON.stringify(item)).movieid);
    });
    MoviesModel.find({}).limit(12).exec((err, doc) => {
      if (err) throw err;
      res.json({
        status: 0,
        ids: ids,
        count: count,
        result: doc
      })
    })
  })
});

router.post('/more', (req, res, next) => {
  let { ids } = req.body;
  if (!ids) {
    res.json({
      status: 1001,
      msg: '参数错误'
    })
  } else {
    let idsarr = ids.split(',');
    idsarr = idsarr.map(item => +item);
    console.log(idsarr)
    MoviesModel.find({movieid: {$in: idsarr}}, (err, doc) => {
      if (err) throw err;
      res.json({
        status: 0,
        result: doc
      })
    })
  }
})

router.post('/detail', (req, res, next) => {
  const { id } = req.body;
  if (!id) {
    res.json({
      status: 1001,
      msg: '参数错误'
    })
  } else {
    DetailsModel.find({id: +id}, (err, doc) => {
      if (err) throw err;
      if (doc.length) {
        res.json({
          status: 0,
          result: doc[0]
        })
      } else {
        res.json({
          status: 0,
          result: {
            "availableEpisodes": 0,
            "awardUrl": "",
            "backgroundColor": "#382740",
            "bingeWatch": 261,
            "bingeWatchst": 0,
            "cat": "剧情",
            "comScorePersona": true,
            "commented": false,
            "dir": "张艺谋",
            "distributions": [
                {
                    "movieScoreLevel": "9-10分",
                    "proportion": "73.51"
                },
                {
                    "movieScoreLevel": "5-8分",
                    "proportion": "19.54"
                },
                {
                    "movieScoreLevel": "1-4分",
                    "proportion": "6.95"
                }
            ],
            "dra": "二十世纪七十年代中期，看电影是人们重要的精神享受。西北某地，没赶上场次的张九声怅然若失，他悄悄从农场溜出来，就是为了看一场电影，那是一盘宣传性质的新闻胶卷带，他相信在那盘胶片中有他已经去世的女儿的“一秒钟”影像。为了找到这胶片，他开始疯狂起来。此时他恰逢流浪儿刘闺女，冤家路窄的两个人，原来同是天涯沦落人。一个是失去女儿的父亲，一个是失去父爱的女儿；一个想要看某一场电影，一个想要12.5米废胶片。目的完全相反，最后却殊途同归，因为一场电影结下了不解之缘。",
            "dur": 104,
            "egg": false,
            "enm": "One Second",
            "episodeDur": 104,
            "episodes": 0,
            "filmAlias": "One Second.Yi miao zhong",
            "fra": "柏林国际电影节",
            "frt": "2019-02-15",
            "globalReleased": true,
            "guideToWish": false,
            "id": 1228788,
            "img": "http://p1.meituan.net/w.h/movie/13cb85d810b0530b951625430c2f35044951112.jpg",
            "latestEpisode": 0,
            "modcsSt": false,
            "movieType": 0,
            "multiPub": true,
            "musicNum": 0,
            "nm": "一秒钟",
            "onSale": true,
            "onlinePlay": false,
            "orderSt": 0,
            "oriLang": "国语",
            "photos": [
                "http://p0.meituan.net/w.h/movie/e1790ae10ffb2d8e9cec38c063d9bd781764585.jpg@2500w_2500h_1l_0e",
                "http://p0.meituan.net/w.h/movie/a799f46d4d225a77eb4ef5cd481609ff1432018.jpg@2500w_2500h_1l_0e",
                "http://p0.meituan.net/w.h/movie/a03e66aaf61f4fe5ed522cfc498f70ae1163359.jpg@2500w_2500h_1l_0e",
                "http://p0.meituan.net/w.h/movie/d462ac696419e1f961087150c5c10f331635814.jpg@2500w_2500h_1l_0e",
                "http://p0.meituan.net/w.h/movie/c7e5e11954d515598d8bc89df72e844f2718103.jpg@2500w_2500h_1l_0e",
                "http://p1.meituan.net/w.h/movie/e53bbfb9daf214b9f1254aa58feb6f9f3341327.jpg@2500w_2500h_1l_0e",
                "http://p0.meituan.net/w.h/movie/06716673ae4e87827cb8d119456bd1643282495.jpg@2500w_2500h_1l_0e",
                "http://p0.meituan.net/w.h/movie/9434578799d1963dac216df91c12657d73926.jpg@2500w_2500h_1l_0e",
                "http://p0.meituan.net/w.h/movie/cda4da4c7cf5ed42e5cd78f01f9ff658100557.jpg@2500w_2500h_1l_0e",
                "http://p1.meituan.net/w.h/movie/c1d954b9cf542dfd07157bda6ebde04291996.jpg@2500w_2500h_1l_0e",
                "http://p0.meituan.net/w.h/movie/ff30cd69f1361eaf60daf70e297383db109955.jpg@2500w_2500h_1l_0e",
                "http://p0.meituan.net/w.h/movie/de50f510827d40c1b89f88706b6cf2a9111556.jpg@2500w_2500h_1l_0e",
                "http://p1.meituan.net/w.h/movie/6b637fd3aadb1efae707ac84937ddb14103825.jpg@2500w_2500h_1l_0e",
                "http://p1.meituan.net/w.h/movie/bd0cdaa73ce80c720fa4b4ab2b7ba34894342.jpg@2500w_2500h_1l_0e",
                "http://p0.meituan.net/w.h/movie/c9d7497b1edfa45e078bad7a4f92f283133420.jpg@2500w_2500h_1l_0e",
                "http://p0.meituan.net/w.h/movie/f620cee43571dc25f31a55b4135be581526204.jpg",
                "http://p1.meituan.net/w.h/movie/02521c34b6740fcc69d9acbf6650ffbf979656.jpg@2500w_2500h_1l_0e",
                "http://p0.meituan.net/w.h/movie/51cef1829629e4fb9e1ee124867cb7bf2336105.png@2500w_2500h_1l_0e",
                "http://p1.meituan.net/w.h/movie/f718e0649179726bf137e0b2d28a1d691253778.jpg@2500w_2500h_1l_0e",
                "http://p0.meituan.net/w.h/movie/4c3bc615b38079bbc9a485d3b4c6cbb91336925.jpg@2500w_2500h_1l_0e"
            ],
            "pn": 65,
            "preScorePersona": false,
            "proScore": 0,
            "proScoreNum": 1,
            "pubDate": 1606406400000,
            "pubDesc": "2020-11-27中国大陆上映",
            "rt": "2020-11-27",
            "sc": 8.7,
            "scm": "张艺谋写给电影的一封情书",
            "scoreLabel": "猫眼购票评分",
            "shareInfo": {
                "channel": 1,
                "content": "简介|二十世纪七十年代中期，看电影是人们重要的精神享受。西北某地，没赶上场次的张九声怅然若失，他悄悄从农场溜出来，就是为了看一场电影，那是一盘宣传性质的新闻胶卷带，他相信在那盘胶片中有他已经去世的女儿的“一秒钟”影像。为了找到这胶片，他开始疯狂起来。此时他恰逢流浪儿刘闺女，冤家路窄的两个人，原来同是天涯沦落人。一个是失去女儿的父亲，一个是失去父爱的女儿；一个想要看某一场电影，一个想要12.5米废胶片。目的完全相反，最后却殊途同归，因为一场电影结下了不解之缘。",
                "img": "http://p1.meituan.net/w.h/movie/13cb85d810b0530b951625430c2f35044951112.jpg",
                "title": "《一秒钟》猫眼购票评分8.7，2020-11-27中国大陆上映",
                "url": "https://m.maoyan.com/asgard/movie/1228788"
            },
            "showst": 3,
            "snum": 28074,
            "src": "中国大陆",
            "star": "张译,刘浩存,范伟",
            "type": 0,
            "vd": "https://vod.pipi.cn/fec9203cvodtransbj1251246104/b5df10a15285890810268900500/v.f42906.mp4",
            "ver": "2D",
            "videoImg": "https://obj.pipi.cn/friday/557cfe75c38f30242428512099bc3794.jpg",
            "videoName": "张艺谋《一秒钟》曝终极预告 张译舍生忘死寻女儿一秒光影记忆",
            "videourl": "https://vod.pipi.cn/fec9203cvodtransbj1251246104/b5df10a15285890810268900500/v.f42906.mp4",
            "viewedSt": 0,
            "vnum": 17,
            "vodFreeSt": 0,
            "vodPlay": false,
            "vodSt": 0,
            "watched": 2534275,
            "wish": 54493,
            "wishst": 0,
            "version": ""
          }
        })
      }
    })
  }
})

module.exports = router;
